package com.mindata.blockchain.core.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

@Slf4j
public class RequestUtil {


    public static void printAll(ServletRequest request, Object obj, Set<String> ignoreParams) {
        HttpServletRequest request2 = (HttpServletRequest) request;

        printAll(request2, obj, ignoreParams);
    }

    public static void printAll(ServletRequest request, Object obj, boolean onlyDebugAble, Set<String> ignoreParams) {
        HttpServletRequest request2 = (HttpServletRequest) request;

        printAll(request2, obj, onlyDebugAble, ignoreParams);
    }

    public static void printAll(HttpServletRequest request, Object obj, Set<String> ignoreParams) {
        printAll(request, obj, true, ignoreParams);
    }

    public static void printAll(HttpServletRequest request, Object obj, boolean onlyDebugAble, Set<String> ignoreParams) {
        if (onlyDebugAble && !log.isDebugEnabled()) {
            return;
        }
        log.debug("------------------------------print request start.------------------------------------");
        log.debug("class:" + obj.getClass());
        String uri = request.getRequestURI();
        log.debug("request uri:" + uri);
        String sessionId = request.getRequestedSessionId();
        log.debug("request sessionId:" + sessionId);
        Map<String, String> headerMap = new HashMap<String, String>();
        Map<String, String> paramMap = new HashMap<String, String>();
        Enumeration<String> headers = request.getHeaderNames();
        while (headers.hasMoreElements()) {
            String headerKey = (String) headers.nextElement();
            String headerValue = request.getHeader(headerKey);
            headerMap.put(headerKey, headerValue);
        }

        Enumeration<String> params = request.getParameterNames();
        while (params.hasMoreElements()) {
            String paramName = (String) params.nextElement();
            String paramValue = request.getParameter(paramName);
            paramMap.put(paramName, paramValue);
        }

        log.debug("header:");
        for (Entry<String, String> entry : headerMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            log.debug(" " + key + ":" + value);
        }
        log.debug("param:");
        for (Entry<String, String> entry : paramMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();

            //敏感字符不打印
            if (!CollectionUtils.isEmpty(ignoreParams) && ignoreParams.contains(key)) {
                value = "****";
            }

            log.debug(" " + key + "=" + value);
        }

        StringBuffer requestURL = request.getRequestURL();

        StringBuffer sb = new StringBuffer();
        sb.append("curl ");
        sb.append("\'").append(requestURL).append("\' ");
        for (Entry<String, String> entry : headerMap.entrySet()) {
            sb.append(" -H ");
            String key = entry.getKey();
            String value = entry.getValue();
//			value = value.replaceAll("=", "=\"").replaceAll(",", "\",");
//			value = value.replaceAll("\'", "'");
            sb.append("\'");
            sb.append(key);
            sb.append(":");
            sb.append(value);
            sb.append("\'");
        }
        sb.append(" --data ");
        sb.append("\"");
        for (Entry<String, String> entry : paramMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            sb.append(key);
            sb.append("=");
            sb.append(value);
            sb.append("&");
        }
        sb.append("\"");
        log.debug(sb.toString());
        log.debug("===============================print request end.===================================");
    }

    public static String getRequestHeader(HttpServletRequest request,
                                          String headerName) {
        String header = request.getHeader(headerName);
        header = StringUtils.defaultIfEmpty(header, "");
        return header;
    }

    public static String getRequestParameter(HttpServletRequest request,
                                             String paramName) {
        String value = request.getParameter(paramName);
        value = StringUtils.defaultIfEmpty(value, "");
        return value;
    }
}
